<?php

/**
 * 从URL获取内容（BET365）
 */
function request_by_curl_365($url, $timeout = 10, $retry = 3, $proxy = '61.153.98.6:8080', $getinfo = false){
	$cookie_str = 'session=processform=0;aps03=lng=10&tzi=27&ct=42&cst=103&cg=0&oty=1;lng=10;pstk=8AFD6236CECB49DCA8A13EB18A4743BB000003';

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
	curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	curl_setopt($ch, CURLOPT_PORT, 443);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_SSLVERSION,3);
	curl_setopt($ch, CURLOPT_COOKIE, $cookie_str);
	curl_setopt($ch, CURLOPT_NOBODY, 0);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
	if (!empty($proxy)){
		curl_setopt($ch, CURLOPT_PROXY, $proxy);
		//curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');
	}
	$data = curl_exec($ch);
	while($data === false && $retry-- > 0){
		usleep(200000);
		$data = curl_exec($ch);
	}
	if ($getinfo){
		$data = curl_getinfo($ch);
		$data['curl_errno'] = curl_errno($ch);
		$data['curl_error'] = curl_error($ch);
	}
	curl_close($ch);
	return $data;
}

/**
 * 从URL获取内容（188BET）
 */
function request_by_curl_188($url, $referer, $timeout = 10, $retry = 3){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
	curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	curl_setopt($ch, CURLOPT_NOBODY, false);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_REFERER, $referer);

	$cookie_file = dirname(dirname(__FILE__)) . "/fetch/tmp/cookies_188bet.txt";
	if (!file_exists($cookie_file)) {
		$fp = fopen($cookie_file, 'w');
		if (!$fp){
			exit('Cookie file create failed.');
		}
		fclose($fp);
	}elseif (!is_writable($cookie_file)) {
		exit('Cookie file not writable.');
	}
	if ($url == $referer){
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // 当会话结束的时候保存一个Cookie
	}else {
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); // Cookie的文件
	}

	$data = curl_exec($ch);
	while($data === false && $retry-- > 0){
		usleep(200000);
		$data = curl_exec($ch);
	}
	//var_dump(curl_error($ch));
	curl_close($ch);
	return $data;
}

function print_log($msg, $exit = true){
	echo date('Y-m-d H:i:s') . "\t" . $msg . "\n";
	if ($exit){
		exit;
	}
}

function check_proxy($proxy){
	$_proxy = explode(':', $proxy);
	if (isset($_proxy[1])){
		if (!is_numeric($_proxy[1]) || $_proxy[1] < 1 || $_proxy[1] > 65535){
			return false;
		}
	}
	$_ip = explode('.', $_proxy[0]);
	if (!isset($_ip[3])
	|| !is_numeric($_ip[0]) || $_ip[0] < 1 || $_ip[0] > 255
	|| !is_numeric($_ip[1]) || $_ip[1] < 1 || $_ip[1] > 255
	|| !is_numeric($_ip[2]) || $_ip[2] < 1 || $_ip[2] > 255
	|| !is_numeric($_ip[3]) || $_ip[3] < 1 || $_ip[3] > 255){
		return false;
	}
	return true;
}

